package com.example.project.Controller;

import com.example.project.Entity.Blog;
import com.example.project.Entity.Like;
import com.example.project.Service.BlogService;
import com.example.project.Service.LikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class BlogController {
    @Autowired
    private BlogService blogService;
    @Autowired
    private LikeService likeService;

    @PostMapping("/saveBlog")
    public Map<String, Object> SaveBlog(@RequestBody Map<String, String> sa_map){
        String author = sa_map.get("author");
        String title = sa_map.get("title");
        String type1 = sa_map.get("type1");
        String type2 = sa_map.get("type2");
        String type3 = sa_map.get("type3");
        String summary = sa_map.get("summary");
        String article = sa_map.get("article");
        String has_picture = sa_map.get("has_picture");
        String picture_url = sa_map.get("picture_url");
        String has_resource = sa_map.get("has_resource");
        String resource_url = sa_map.get("resource_url");
        Map<String, Object> map = new HashMap<>();
        try{
            if(title.length()==0){
                map.put("errno", "4001");
                map.put("message", "请先输入文章标题！");
                return map;
            }
            if(article.length()==0){
                map.put("errno", "4002");
                map.put("message", "请先输入文章内容！");
                return map;
            }
            Blog blog1 = new Blog(author, title, type1, type2, type3, summary, article, "待发布", has_picture, picture_url, has_resource, resource_url);
            blogService.SaveBlog(blog1);
            map.put("errno", "1");
            map.put("message", "文章保存成功！");
        } catch (Exception e){
            map.put("errno", "4003");
            map.put("message", "文章保存失败！");
        }
        return map;
    }

    @PostMapping("/deleteBlog")
    public Map<String, Object> DeleteBlog(@RequestBody Map<String, String> de_map){
        int blog_id = Integer.parseInt(de_map.get("blog_id"));
        Map<String, Object> map = new HashMap<>();
        try{
            blogService.deleteBlog(blog_id);
            map.put("errno", "1");
            map.put("message", "删除文章成功！");
        } catch (Exception e){
            map.put("errno", "5001");
            map.put("message", "删除文章失败！");
        }
        return map;
    }

    @PostMapping("/publishBlog")
    public Map<String, Object> PublicBlog(@RequestBody Map<String, String> pu_map){
        String author = pu_map.get("author");
        String title = pu_map.get("title");
        String type1 = pu_map.get("type1");
        String type2 = pu_map.get("type2");
        String type3 = pu_map.get("type3");
        String summary = pu_map.get("summary");
        String article = pu_map.get("article");
        String has_picture = pu_map.get("has_picture");
        String picture_url = pu_map.get("picture_url");
        String has_resource = pu_map.get("has_resource");
        String resource_url = pu_map.get("resource_url");
        Map<String, Object> map = new HashMap<>();
        try{
            if(title.length()==0){
                map.put("errno", "6001");
                map.put("message", "请先输入文章标题！");
                return map;
            }
            if(article.length()==0){
                map.put("errno", "6002");
                map.put("message", "请先输入文章内容！");
                return map;
            }
            Blog blog1 = new Blog(author, title, type1, type2, type3, summary, article, "已发布", has_picture, picture_url, has_resource, resource_url);
            blogService.SaveBlog(blog1);
            map.put("errno", 1);
            map.put("message", "文章发布成功！");
        } catch (Exception e){
            map.put("errno", "6003");
            map.put("message", "文章发布失败！");
        }
        return map;
    }

    @PostMapping("/backBlog")
    public Map<String, Object> BackBlog(@RequestBody Map<String, String> ba_map){
        int blog_id = Integer.parseInt(ba_map.get("blog_id"));
        Map<String, Object> map = new HashMap<>();
        try{
            blogService.backBlog(blog_id);
            map.put("errno", "1");
            map.put("message", "文章撤回成功！");
        } catch (Exception e) {
            map.put("errno", "7001");
            map.put("message", "文章撤回失败！");
        }
        return map;
    }

    @PostMapping("/selectBlog")
    public Map<String, Object> SelectBlog(@RequestBody Map<String, String> se_map){
        int blog_id = Integer.parseInt(se_map.get("blog_id"));
        Map<String, Object> map = new HashMap<>();
        try{
            Blog blog1 = blogService.SelectBlogById(blog_id);
            if(blog1==null){
                map.put("errno", "8001");
                map.put("message", "文章不存在！");
                return map;
            }
            String article = blog1.getArticle();
            map.put("errno", "1");
            map.put("message", article);
        } catch (Exception e){
            map.put("errno", "8002");
            map.put("message", "文章获取失败！");
        }
        return map;
    }

    @PostMapping("/thumbUp")
    public Map<String, Object> ThumbUp(@RequestBody Map<String, String> up_map){
        int user_id = Integer.parseInt(up_map.get("user_id"));
        int blog_id = Integer.parseInt(up_map.get("blog_id"));
        Map<String, Object> map = new HashMap<>();
        try{
            Like like1 = new Like(user_id, blog_id);
            likeService.addLike(like1);
            map.put("errno", "1");
            map.put("message", "点赞文章成功！");
        } catch (Exception e){
            map.put("errno", "9001");
            map.put("message", "点赞文章失败！");
        }
        return map;
    }

    @PostMapping("/thumbDown")
    public Map<String, Object> ThumbDown(@RequestBody Map<String, String> do_map){
        int user_id = Integer.parseInt(do_map.get("user_id"));
        int blog_id = Integer.parseInt(do_map.get("blog_id"));
        Map<String, Object> map = new HashMap<>();
        try{
            likeService.subLike(user_id, blog_id);
            map.put("errno", "1");
            map.put("message", "取消文章点赞成功！");
        } catch (Exception e){
            map.put("errno", "10001");
            map.put("message", "取消文章点赞失败！");
        }
        return map;
    }
    @PostMapping("/clickBlog")
    public Map<String, Object> ClickBlog(@RequestBody Map<String, String> do_map){
        int blog_id = Integer.parseInt(do_map.get("blog_id"));
        Map<String, Object> map = new HashMap<>();
        try{
            blogService.addClick(blog_id);
            map.put("errno", "1");
            map.put("message", "点击文章文章成功！");
        } catch (Exception e){
            map.put("errno", "17001");
            map.put("message", "点击文章失败！");
        }
        return map;
    }
}
